package com.example.myapplication7;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.util.Log;

public class IntentConnectionReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        ConnectivityManager cm = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
        boolean isConnected = activeNetwork != null && activeNetwork.isConnectedOrConnecting();
//            判断是否有网络连接
        if(isConnected){
            Log.d("onReceive: ", "有网");
            //判断当前网络是流量还是WIFI
            boolean isWiFi = activeNetwork.getType() == ConnectivityManager.TYPE_WIFI;
            boolean isMobile = activeNetwork.getType() == ConnectivityManager.TYPE_MOBILE;
            if(isWiFi){
                Log.d("当前网络状态: ", "WIFI");
            }else  if(isMobile){
                Log.d("当前网络状态: ", "数据流量");
            }
        }else {
            Log.d("onReceive: ","没网");
        }
    }
}

